package ssi.beans;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.struts.action.ActionForm;

import ssi.beans.User;
import ssi.tools.Connector;

import ssi.tools.Rows;

public class Message extends ActionForm {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id = -1;
	private String title = "";
	private String text = "";
	private int senderId = -1;
	private int receiverId = -1;
	private String sentDate;
	private String[] sender;
	private String[] receiver;
	
	public Message(int id, String title, String text, int senderId, int receiverId, String sentDate){
		this.id = id;
		this.title = title;
		this.text = text;
		this.senderId = senderId;
		this.receiverId = receiverId;
		this.sender = User.getUser(senderId+"");
		this.receiver = User.getUser(receiverId+"");
		this.sentDate=sentDate;
	}
	
	public Message(){
		
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getSenderId() {
		return senderId;
	}

	public void setSenderId(int senderId) {
		this.senderId = senderId;
	}

	public int getReceiverId() {
		return receiverId;
	}

	public void setReceiverId(int receiverId) {
		this.receiverId = receiverId;
	}

	public String getSentDate() {
		return sentDate;
	}

	public void setSentDate(String sentDate) {
		this.sentDate = sentDate;
	}

	public String[] getSender() {
		return sender;
	}

	public void setSender(String[] sender) {
		this.sender = sender;
	}

	public String[] getReceiver() {
		return receiver;
	}

	public void setReceiver(String[] receiver) {
		this.receiver = receiver;
	}
	
public static Message[] getInboxMessages(int userId) {
		
		Message[] messages = null;
		
		try {
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			ResultSet result = statement.executeQuery("SELECT * FROM `messages` WHERE `receiver_id`=" + userId + "");
			
			messages = new Message[Rows.getRows(result)];
			int counter = 0;
			
			while (result.next()) {
				messages[counter++] = new Message(result.getInt("id"), result.getString("title"), result.getString("text"), result.getInt("sender_id"), result.getInt("receiver_id"), result.getString("sent_date"));
			}
			
			connection.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return messages;
	}
	
	public static Message[] getOutboxMessages(int userId) {
		
		Message[] messages = null;
		
		try {
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			ResultSet result = statement.executeQuery("SELECT * FROM `messages` WHERE `sender_id`=" + userId + "");
			
			messages = new Message[Rows.getRows(result)];
			int counter = 0;
			
			while (result.next()) {
				messages[counter++] = new Message(result.getInt("id"), result.getString("title"), result.getString("text"), result.getInt("sender_id"), result.getInt("receiver_id"), result.getString("sent_date"));
			}
			
			connection.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return messages;
	}
	
	public static Message getMessage(int messageId) {
		
		Message message = null;
		
		try {
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			System.out.println("SELECT * FROM `messages` WHERE `id`=" + messageId);
			ResultSet result = statement.executeQuery("SELECT * FROM `messages` WHERE `id`=" + messageId);
			while (result.next()) {
				message = new Message(result.getInt("id"), result.getString("title"), result.getString("text"), result.getInt("sender_id"), result.getInt("receiver_id"), result.getString("sent_date")); 
			}
			
			connection.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return message;
	}
	
	
}
